﻿using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
using Entity;

namespace IRepository
{
    public interface IWarehouseRepository
    {
        /// <summary>
        /// 多条件分页查询仓库（基于PaginationQuery）
        /// </summary>
        Task<PaginationResult<WarehouseEntity>> GetPageListAsync(
            string warehouseName,
            int? warehouseStatus,
            int? organizationId,
            string warehouseManager,
            PaginationQuery pagination // 分页参数实体
        );
        Task<WarehouseEntity> GetByNameAsync(string name);
        Task UpdateAsync(WarehouseEntity entity);
        Task UpdateAsync(WarehouseEntity entity, IDbTransaction transaction);
        Task AddAsync(WarehouseAddDTO dto);
        /// <summary>
        /// 查询仓库状态列表（从wms_basic_warehouse表获取distinct状态）
        /// </summary>
        Task<List<WarehouseStatusDTO>> GetWarehouseStatusListAsync();
        /// <summary>
        /// 删除仓库（软删除）
        /// </summary>
        Task DeleteAsync(int warehouseId);
        /// <summary>
        /// 批量更新仓库状态（锁定/启用/停用）
        /// </summary>
        /// <param name="warehouseIds">仓库ID列表</param>
        /// <param name="status">目标状态（0=停用，1=启用，2=锁定）</param>
        /// <returns>影响行数</returns>
        Task<int> BatchUpdateStatusAsync(List<int> warehouseIds, string status);
        /// <summary>
        /// 根据ID集合查询仓库
        /// </summary>
        Task<List<WarehouseEntity>> GetByIdsAsync(List<int> ids);
        /// <summary>
        /// 检查仓库是否有库存（用于锁定前校验）
        /// </summary>
        Task<List<string>> CheckWarehousesHasStockAsync(List<int> warehouseIds);
        /// <summary>
        /// 根据ID查询仓库（用于详情）
        /// </summary>
        Task<WarehouseEntity> GetByIdAsync(int warehouseId);

        /// <summary>
        /// 查询指定仓库的库位数量
        /// </summary>
        Task<int> GetLocationCountByWarehouseIdAsync(int warehouseId);
        /// <summary>
        /// 检查仓库编号是否存在（编辑时排除自身ID）
        /// </summary>
        Task<bool> ExistsNumberAsync(string number, int excludeId);

        /// <summary>
        /// 编辑仓库（包含库位关联更新）
        /// </summary>
        Task UpdateWarehouseAsync(WarehouseEditDTO dto);
        // 新增：获取所有仓库
        Task<List<WarehouseEntity>> GetAllAsync();
        Task<List<StorageLocationEntity>> GetAllStorageLocationsAsync();
        Task<List<StorageLocationEntity>> GetStorageLocationsByWarehouseAsync(string warehouseName);
        // 测试方法
        Task<bool> TestStorageLocationConnection();
        // 根据仓库ID获取第一个可用货位（location_status=1）
        Task<StorageLocationEntity> GetFirstValidStorageLocationAsync(int warehouseId);
        // 新增：支持传入事务
        Task<StorageLocationEntity> GetFirstValidStorageLocationAsync(int warehouseId, IDbTransaction transaction);
        Task<WarehouseEntity> GetByIdAsync(int warehouseId, IDbTransaction transaction = null);
        Task<WarehouseEntity> GetByNameAsync(string name, IDbTransaction transaction);

        Task<int?> GetValidLocationIdByWarehouseIdAsync(int warehouseId, IDbTransaction transaction = null);

        Task<string> GetLocationNameByLocationIdAsync(int locationId, IDbTransaction transaction = null);
    }
}